package com.hb.ssmdemo.tools.udp;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;

import com.hb.ssmdemo.tools.SpringUtils;

public class JDBCUtils{

	private static Logger log = Logger.getLogger(JDBCUtils.class);

    public static JdbcTemplate getJdbcTemplate() {
        return (JdbcTemplate) SpringUtils.getBean("jdbcTemplate");
    }
	
	/**
	 * 
	 * @Description: 查询出所有的UDP配置信息
	 * 
	 */
	public static List<Udpset> queryAllUdpsetInfo(){
		
		String sql = "select u.* from irs_udpset u";
		List<?> list = JDBCUtils.getJdbcTemplate().queryForList(sql);
		List<Udpset> udpList = new ArrayList<Udpset>();
		for(int i = 0; i < list.size(); i++){
			Udpset model = new Udpset();
			model.setLocalhost(((Map<?,?>)list.get(i)).get("localhost").toString());
			model.setLocalRPort(((Map<?,?>)list.get(i)).get("localRPort").toString());
			model.setServerip(((Map<?,?>)list.get(i)).get("serverip").toString());
			model.setNetport(((Map<?,?>)list.get(i)).get("netport").toString());
			udpList.add(model);
		}
		return udpList;
	}
	
	/**
	 * 
	 * @Description: 存储UDP自动上传的数据 
	 * 
	 */
	public static void callAutoUdpData(String frame,String terCode,String use,Integer curStatus,Integer norStatus,Integer strategy,Float temperature,Float humidity,Float condensation,Integer sensorOneValue,Integer sensorTwoValue){
		Connection con = null;
		try {
			con = getJdbcTemplate().getDataSource().getConnection();
			//调用存储过程
			CallableStatement cs = con.prepareCall("{call auto_udp_data(?,?,?,?,?,?,?,?,?,?,?)}");
			cs.execute();
			cs.close();
			con.close();
		} catch (SQLException e) {
			log.info(e.getMessage());
		} finally {
			try {
				if(con.isClosed()){
					con.close();
				}
			} catch (SQLException e) {
				log.info(e.getMessage());
			}
		}
	}
}
